package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test6 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        String packName = Test6.class.getPackage().getName();
        File dir = new File(Test6.class.getResource(".").toURI());
        for(File sub : dir.listFiles(f->f.getName().endsWith(".class"))){
            Class cls = Class.forName(packName+"."+sub.getName().replace(".class",""));
            //判断该类是否被注解@AutoRunClass标注
            if(cls.isAnnotationPresent(AutoRunClass.class)){
                //实例化
                System.out.println("实例化:"+cls.getSimpleName());
                Object obj = cls.newInstance();
                //获取所有方法
                for(Method method:cls.getDeclaredMethods()){
                    //判断该方法是否被注解@AutoRunMethod标注
                    if(method.isAnnotationPresent(AutoRunMethod.class)){
                        System.out.println("调用方法"+method.getName()+"()");
                        method.invoke(obj);
                    }
                }
            }
        }
    }
}
